//
//  RootViewController.m
//  ImitateJDjrApp
//
//  Created by srj on 2018/6/6.
//  Copyright © 2018年 168licai. All rights reserved.
//

#import "RootViewController.h"
#import "AppDelegate.h"


@interface RootViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic) UIScrollView *bgScrollView;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self createUI];
}

- (void)createUI
{
    [self.view addSubview:self.bgScrollView];
    [self.view addSubview:self.topScrollView];
    
    [self addChildViewController:self.firstVC];
    self.firstVC.view.frame = CGRectMake(0, 0, kUISCREEN_WIDTH, self.bgScrollView.frame.size.height);
    [self.bgScrollView addSubview:self.firstVC.view];//把子控制器的 view 添加到父控制器的 view 上面
    
    [self addChildViewController:self.secondVC];
    self.secondVC.view.frame = CGRectMake(kUISCREEN_WIDTH, 0, kUISCREEN_WIDTH, self.bgScrollView.frame.size.height);
    [self.bgScrollView addSubview:self.secondVC.view];
    
    [self addChildViewController:self.thirdVC];
    self.thirdVC.view.frame = CGRectMake(kUISCREEN_WIDTH*2, 0, kUISCREEN_WIDTH, self.bgScrollView.frame.size.height);
    [self.bgScrollView addSubview:self.thirdVC.view];
    
    self.bgScrollView.contentSize = CGSizeMake(3*kUISCREEN_WIDTH, kUISCREEN_HEIGHT-kTopHeight-kBottomHeight);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.topScrollView){
        [self.bgScrollView setContentOffset:CGPointMake((scrollView.contentOffset.x)*kUISCREEN_WIDTH/kTOPSCROLLVIEWWIDTH, 0)];

    }
    if (scrollView == self.bgScrollView){
        [self.topScrollView setContentOffset:CGPointMake((scrollView.contentOffset.x)*kTOPSCROLLVIEWWIDTH/kUISCREEN_WIDTH, 0)];
    }
}
- (UIScrollView *)bgScrollView
{
    if (!_bgScrollView) {
        _bgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kUISCREEN_WIDTH, kUISCREEN_HEIGHT)];
        _bgScrollView.contentSize = CGSizeMake(0, kUISCREEN_HEIGHT-kTopHeight-kBottomHeight);
        _bgScrollView.backgroundColor = [UIColor blueColor];
        _bgScrollView.pagingEnabled = YES;
        _bgScrollView.bounces = NO;
        _bgScrollView.delegate = self;
        
    }
    return _bgScrollView;
}

-(UIScrollView *)topScrollView{
    if (!_topScrollView ){
        _topScrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
        _topScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(kTOPSCROLLVIEWSPACE, 0, kTOPSCROLLVIEWWIDTH, 200)];
        _topScrollView.contentSize = CGSizeMake((kTOPSCROLLVIEWWIDTH)*3, 0);
        _topScrollView.backgroundColor = [UIColor redColor];
        _topScrollView.pagingEnabled = YES;
        _topScrollView.bounces = NO;
        _topScrollView.clipsToBounds  = NO;
        _topScrollView.delegate = self;
        _topScrollView.showsHorizontalScrollIndicator = NO;
        for (int i = 0; i<3; i++) {
            UIView *v = [[UIView alloc] initWithFrame:CGRectMake(i*kTOPSCROLLVIEWWIDTH, 0, kTOPSCROLLVIEWWIDTH, 200)];
            v.layer.cornerRadius = 10;
            v.backgroundColor = [UIColor greenColor];
            
            
            [_topScrollView addSubview:v];
        }
        [self.bgScrollView addSubview:_topScrollView];
        
    }
    return _topScrollView;
}

- (FirstViewController *)firstVC
{
    if (!_firstVC){
        _firstVC = [[FirstViewController alloc] init];
    }
    return _firstVC;
}
- (SecondViewController *)secondVC
{
    if (!_secondVC){
        _secondVC = [[SecondViewController alloc] init];
    }
    return _secondVC;
}
- (ThirdViewController *)thirdVC
{
    if (!_thirdVC){
        _thirdVC = [[ThirdViewController alloc] init];
    }
    return _thirdVC;
}


@end
